angular.module('BaseControl')

//页面引用该控件时，value如果希望反向同步到控件绑定value时，必须与主Model的对象的属性进行绑定，因为隔离作用域的问题，不知道是什么原因，暂时未能解决
.directive("checkbox", [function() {
    return {
        restrict: "E",	// 指令是一个元素 (并非属性)，备选项有"A"、"E" 和 "C"， "M" ,分别代表attribute、element、class和comment（默认值为"A"）
        scope: {
            value: '=',
            change: '&',
            blur: '&'
        },
        template:
            '<input type="checkbox" class="CheckBox" ng-model="value" ng-blur="textBlur()" ng-change="valueChange()" ng-keydown="keyDown()" ng-show="Visible" ng-disabled="!Enabled"/>'
        ,
        replace: true,			// 使用模板替换原始标记
        transclude: true,		// 不复制原始HTML内容
        controller: ['$scope', function($scope){
            $scope.imgPath;

            $scope.valueChange = function(){
                window.setTimeout(function(){ 
                    $scope.change();
                    $scope.$parent.$digest();
                }, 1);
            };
            $scope.textBlur = function(){
                 $scope.blur();
            }
            $scope.keyDown = function(){
                if(event.keyCode == 32){
                    $scope.value = !$scope.value;
                    window.setTimeout(function(){ 
                        $scope.change();
                        $scope.$parent.$digest();
                    }, 1);
                    event.preventDefault();
                }
            };
        }],
        link: function (scope, element, attrs) {
            scope.Enabled = true;
            scope.Visible = true;

            scope.$watch(
                function(){
                    return attrs["enabled"];
                },
                function(){
                    if (attrs["enabled"] != undefined && attrs["enabled"].trim().toLocaleLowerCase() == "false") {
                        scope.Enabled = false;
                    }
                    else{
                        scope.Enabled = true;
                    }
                }
            );

            scope.$watch(
                function(){
                    return attrs["visible"]
                },
                function(){
                    if (attrs["visible"] != undefined && attrs["visible"].trim().toLocaleLowerCase() == "false") {
                        scope.Visible = false;
                    }
                    else{
                        scope.Visible = true;
                    }
                }
            );
        }
    }
}]);
